<% modal_content = capture do %>
  <% if sections.present? %>
    <% sections.each do |section| %>
      <%= section %>
    <% end %>
  <% end %>

  <% if content.present? %>
    <% if @sectioned %>
      <%= render Polaris::Modal::SectionComponent.new do %>
        <%= content %>
      <% end %>
    <% else %>
      <%= content %>
    <% end %>
  <% end %>
<% end %>

<%= render(Polaris::BaseComponent.new(**system_arguments)) do %>
  <div>
    <%= render(Polaris::BaseComponent.new(**dialog_arguments)) do %>
      <div class="<%= modal_classes %>">
        <% if @title.present? %>
          <%= polaris_box(
            classes: "Polaris-Modal-Header",
            padding_block_start: "2",
            padding_block_end: "2",
            padding_inline_start: "4",
            padding_inline_end: "4",
            border_block_end_width: "1",
            border_color: "border-subdued",
            background: "bg-subdued"
          ) do %>
            <%= polaris_horizontal_grid(
              classes: "Polaris-Modal-Header__Title",
              columns: {xs: "1fr auto"},
              gap: "4"
            ) do %>
              <%= polaris_horizontal_stack(gap: "4", block_align: :center) do %>
                <%= polaris_text(as: :h2, variant: :headingMd, break_word: true) do %>
                  <%= @title %>
                <% end %>
              <% end %>
              <% if close_button.present? %>
                <%= close_button %>
              <% else %>
                <%= render(Polaris::BaseComponent.new(**close_button_arguments)) do %>
                  <%= polaris_icon(name: "XIcon", color: :base) %>
                <% end %>
              <% end %>
            <% end %>
          <% end %>
        <% end %>

        <% if @scrollable %>
          <%= polaris_scrollable(classes: "Polaris-Modal__Body") do %>
            <%= modal_content %>
          <% end %>
        <% else %>
          <%= modal_content %>
        <% end %>

        <% if render_footer? %>
          <%= polaris_horizontal_stack(classes: "Polaris-Modal-Footer", gap: "4", block_align: :center) do %>
            <%= polaris_box(
              border_color: "border-subdued",
              border_block_start_width: "1",
              min_height: "var(--p-space-12)",
              padding: "4",
              width: "100%",
            ) do %>
              <%= polaris_stack(alignment: :center) do |stack| %>
                <% stack.with_item(fill: true) do %>
                <% end %>

                <% stack.with_item do %>
                  <%= polaris_button_group do |group| %>
                    <% if secondary_actions %>
                      <% secondary_actions.each do |action| %>
                        <% group.with_item do %>
                          <%= action %>
                        <% end %>
                      <% end %>
                    <% end %>

                    <% if primary_action %>
                      <% group.with_item do %>
                        <%= primary_action %>
                      <% end %>
                    <% end %>
                  <% end %>
                <% end %>
              <% end %>
            <% end %>
          <% end %>
        <% end %>
      </div>
    <% end %>
  </div>
<% end %>
